qinglan.Canvas.ResourceLoader = qinglan.Canvas.Class(function() {
	this.imgList = {};
	this.soundList = {};
}).methods({
	loadImg: function(srcList, callback) {
		var self = this;
		qinglan.Canvas.ResourceLoader.loadImgs(srcList,function(img,src,persent,loaded){
			self.imgList[src] = img;
			callback && callback(img,persent,loaded);
		});
	},
	loadSound: function() {

	},
	getImgById: function(id) {
		return this.imgList[id] || null;
	},
	getSoundById: function(id) {
		return this.soundList[id] || null;
	}

}).statics({
	imgPool: {},
	soundPool: {},
	loadImgs: function(srcList,callback) {
		var imgs = [];
		if(qinglan.Type.isArray(srcList)){
			imgs = srcList;
		}else{
			var args = qinglan.Argument.toArray(arguments);
			qinglan.Object.each(args,function(arg){
				qinglan.Type.isString(arg) && imgs.push(arg);
				if (qinglan.Type.isFunction(arg)) {
					return false;
				}
			});
		}
		var len = imgs.length;
		var count = 0;
		var basePath = qinglan.Canvas.Config.resource.imgBasePath;
		var onload = function(e,src) {
			count++;
			callback && callback(this,src,count/len,true);
		};
		var onerror = function(e) {
			count++;
			callback && callback(this,count/len,false);
		};
		qinglan.Object.each(imgs,function(src){
			var i = qinglan.Canvas.ResourceLoader.getImgById(src);
			if(i){
				onload(i);
			}else{
				i = new Image();
				i.onload = function(e){
					onload.call(this,e,src);
				};
				i.onerror = onerror;
				i.src = basePath + src; 
				qinglan.Canvas.ResourceLoader.imgPool[src] = i;
			}

		});
	},
	getImgById: function(id) {
		return qinglan.Canvas.ResourceLoader.imgPool[id] || null;
	},
	getSoundById: function(id) {
		return qinglan.Canvas.ResourceLoader.soundPool[id] || null;
	}
});

qinglan.Type.isSound = function(src) {
	return /png|jpg|jpeg|gif|bmp$/.test(src.substr(src.lastIndexOf('.')));
};

qinglan.Type.isImg = function(src) {
	return /ogg|mp3|m4a|wav$/.test(src.substr(src.lastIndexOf('.')));
};
